package GUI; //done

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Rectangle;
import java.awt.TextArea;
import java.awt.SystemColor;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;

import Client.ConnectionToServer;
import Common.Settings;
import Entities.Product;

import java.awt.Font;
import java.io.File;
import java.nio.file.Files;

/**
 * This Class controls Product Window.
 * @author Ameer
 * @author waleed
 * @author bshara
 * @author kamil
 */
public class ProductWindow extends JPanel{
	
	/** The serial Version UID. */
	private static final long serialVersionUID = 1L;

	/** The Product Name Label. */
	private JLabel pNameLabel = null;
	
	/** The Product Price Label. */
	private JLabel pPriceLabel = null;
	
	/** The Product description Label. */
	private JLabel pDescLabel = null;
	
	/** The Product Number Label. */
	private JLabel pCNumlLabel = null;
	
	/** The Product Name Text Field. */
	private JTextField pNameField = null;
	
	/** The Product Price Text Field. */
	private JTextField pPriceTextField = null;
	
	/** The Product Description Text Area. */
	private TextArea pDescTextArea = null;
	
	/** The Product Number Text Field. */
	private JTextField pCnumTextField = null;
	
	/** The Save Buttons. */
	private JButton saveButton = null;
	
	/** The Cancel Buttons. */
	private JButton cancelButton = null;
	
	/** The Update Buttons. */
	private JButton updateButton = null;
	
	/** The Remove Buttons. */
	private JButton removeButton = null;
	
	/** The Product. */
	private Product product = null;
	
	/** Lables For Background and Pictures. */
	private JLabel label_1;
	
	/** The Product Picture Link. */
	protected String prodPic;

	/** The Show Product Picture Button. */
	private JButton showPictureButton;

	/**
	 * The default constructor
	 */
	public ProductWindow(){
		super();
		setBackground(SystemColor.activeCaption);
		initialize();	
	}
	
	/**
	 * This function initializes ProductWindow
	 */
	private void initialize() {
		pCNumlLabel = new JLabel();
		pCNumlLabel.setFont(new Font("Tahoma", Font.BOLD, 13));
		pCNumlLabel.setBounds(new Rectangle(30, 18, 169, 22));
		pCNumlLabel.setText("Product ID:");
		
		pNameLabel = new JLabel();
		pNameLabel.setFont(new Font("Tahoma", Font.BOLD, 13));
		pNameLabel.setBounds(new Rectangle(31, 46, 168, 22));
		pNameLabel.setText("Product name:");
		
		pPriceLabel = new JLabel();
		pPriceLabel.setFont(new Font("Tahoma", Font.BOLD, 13));
		pPriceLabel.setBounds(new Rectangle(31, 74, 168, 22));
		pPriceLabel.setText("Product price:");
		
		pDescLabel = new JLabel();
		pDescLabel.setFont(new Font("Tahoma", Font.BOLD, 13));
		pDescLabel.setBounds(new Rectangle(31, 135, 168, 22));
		pDescLabel.setText("Product description:");
			
		product = new Product();
		
		this.setSize(400, 350);
		this.setLayout(null);
		this.add(pCNumlLabel, null);
		this.add(pNameLabel, null);
		this.add(pPriceLabel, null);
		this.add(pDescLabel, null);
		this.add(getPCnumTextField(), null);
		this.add(getPNameTextField(), null);
		this.add(getPPriceTextField(), null);
		this.add(getPDescpDescTextArea(), null);
		this.add(getcancelButton(),null);
		this.add(getLabel_1());
		
		JButton btnAddPicture = new JButton("Add Picture");
		btnAddPicture.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser chooser = new JFileChooser();
				chooser.setCurrentDirectory(new java.io.File("."));
				chooser.setDialogTitle("choosertitle");
				chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
				chooser.setAcceptAllFileFilterUsed(false);
				chooser.addChoosableFileFilter(new FileNameExtensionFilter("Images", "jpg", "png", "gif", "bmp"));
				chooser.showOpenDialog(null);
				
				File file = chooser.getSelectedFile();
				prodPic = file.getPath();

			if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
				  System.out.println("getCurrentDirectory(): " + chooser.getCurrentDirectory());
				  System.out.println("getSelectedFile() : " + chooser.getSelectedFile()); 
				} 
			else {
				  System.out.println("No Selection ");
				}
				
				
			}
		});
		btnAddPicture.setBounds(209, 102, 130, 26);
		add(btnAddPicture);
		
		JLabel lblProductPicture = new JLabel();
		lblProductPicture.setText("Product Picture:");
		lblProductPicture.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblProductPicture.setBounds(new Rectangle(31, 135, 168, 22));
		lblProductPicture.setBounds(31, 102, 168, 22);
		add(lblProductPicture);
		
	}
	
	/**
	 * This function initializes pCnumTextField	
	 */
	private JTextField getPCnumTextField(){
		if (pCnumTextField == null) {
			pCnumTextField = new JTextField();
			pCnumTextField.setBounds(new Rectangle(209, 18, 130, 23));
		}
		return pCnumTextField;
	}

	/**
	 * This function initializes pNameTextField	
	 */
	private JTextField getPNameTextField(){
		if (pNameField == null) {
			pNameField = new JTextField();
			pNameField.setBounds(new Rectangle(209, 45, 130, 25));
		}
		return pNameField;
	}
	
	/**
	 * This function initializes pPriceTextField
	 */
	private JTextField getPPriceTextField(){
		if (pPriceTextField == null) {
			pPriceTextField = new JTextField();
			pPriceTextField.setBounds(new Rectangle(208, 73, 130, 25));
		}
		return pPriceTextField;
	}

	/**
	 * This function initializes PDescpDescTextArea	
	 */
	private TextArea getPDescpDescTextArea(){
		if (pDescTextArea == null) {
			pDescTextArea = new TextArea();
			pDescTextArea.setBounds(new Rectangle(208, 135, 131, 52));			
		}
		return pDescTextArea;
	}
	
	/**
	 * This function initializes SaveButton	
	 */
	private JButton getSaveButton() {
		if (saveButton == null) {
			saveButton = new JButton();
			saveButton.setBounds(new Rectangle(150, 200, 110, 29));
			saveButton.setText("ADD");
			saveButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					String msgfromserver = null;
					try {
						long prodID = Long.parseLong(pCnumTextField.getText());
						String productname= pNameField.getText();
						double price = Double.parseDouble(pPriceTextField.getText());
						String description = pDescTextArea.getText();
						if (!description.equals("") &&  !productname.equals("") && prodID > 0)
							if (price > 0){
								ConnectionToServer connectiontoserver = new ConnectionToServer();
								Product product = new Product();
								product.setDescription(description);
								product.setName(productname);
								product.setPrice(price);
								product.setProdID(prodID);
								product.setProdPic(prodPic);
								msgfromserver = (String)connectiontoserver.addProduct(product);
								if (msgfromserver != null){
									if (msgfromserver.equals("OK"))
										JOptionPane.showMessageDialog(null, "The product successfully saved");
									if (msgfromserver.equals("IlegalCategorynumber"))
										JOptionPane.showMessageDialog(null, "ERROR: Ilegal Category number");								
								}
								else
									JOptionPane.showMessageDialog(null, "FATAL ERROR");
							}
							else
								JOptionPane.showMessageDialog(null, "Ilegal price");				
						else{
							JOptionPane.showMessageDialog(null, "Requested fields not filled");
						}
						
					}
					catch (Exception excp){
						JOptionPane.showMessageDialog(null, "ILEGAL INPUT");	
					}	
				}
			});			
		}
		return saveButton;
	}
	
	/**
	 * This function initializes removeButton	
	 */	
	private JButton getRemoveButton(){
		if (removeButton == null){
			removeButton = new JButton();
			removeButton.setText("Remove");
			removeButton.setBounds(new Rectangle(150, 247, 110, 29));
			removeButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					String msgfromserver = null;
					try {
						ConnectionToServer connection_To_server = new ConnectionToServer();
						msgfromserver = (String)connection_To_server.removeProduct(product.getProdID());
						if (msgfromserver.equals("OK"))
							JOptionPane.showMessageDialog(null, "The product successfully remove");
						if (msgfromserver.equals("ERROR"))
							JOptionPane.showMessageDialog(null, "ERROR");					
					} 
					catch (Exception e1){
						JOptionPane.showMessageDialog(null, "FATAL ERROR");
						}
				}
			});			
	  }
		return removeButton;				
	}
	
	/**
	 * This function load product to form
	 * @param product
	 */
	public void loadProduct(Product product){
		this.product = product;
		pDescTextArea.setText(this.product.getDescription());
		pPriceTextField.setText(String.valueOf(this.product.getPrice()));
		pNameField.setText(this.product.getName());
		pCnumTextField.setText(String.valueOf(this.product.getProdID()));
	}
	
	/**
	 * This function initializes updateButton	
	 */	
	private JButton getUpdateButton(){
		if (updateButton == null){
			updateButton = new JButton();
			updateButton.setText("Update");
			updateButton.setBounds(new Rectangle(150, 247, 110, 29));
			updateButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					try {
						String description = pDescTextArea.getText();
						double price = Double.parseDouble(pPriceTextField.getText());
						String productname= pNameField.getText();
						if (!description.equals("") &&  !productname.equals(""))
							if (price > 0){
								product.setDescription(description);
								product.setName(productname);
								product.setPrice(price);
								product.setProdPic(prodPic);
								String msgfromserver = product.updateProduct();
								if (msgfromserver != null){
									if (msgfromserver.equals("OK"))
										JOptionPane.showMessageDialog(null, "The product successfully updated");
									if (msgfromserver.equals("IlegalCategorynumber"))
										JOptionPane.showMessageDialog(null, "ERROR: Ilegal Category number");								
								}
								else
									JOptionPane.showMessageDialog(null, "FATAL ERROR");
							}
							else
								JOptionPane.showMessageDialog(null, "Ilegal price");				
						else{
							JOptionPane.showMessageDialog(null, "Requested fields not filled");
						}
					
					} 
					catch (Exception excp){
						JOptionPane.showMessageDialog(null, "ILEGAL INPUT");	
					}	
				}
		});
	  }
		return updateButton;				
	}
	
	/**
	 * This function initializes cancelButton
	 */	
	public JButton getcancelButton(){
		if (cancelButton == null){
			cancelButton = new JButton();
			cancelButton.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent arg0){	
				}
			});
			cancelButton.setText("Back");
			cancelButton.setBounds(new Rectangle(14, 247, 100, 29));
		}	
		return cancelButton;				
	}
	
	/**
	 * This function initializes ShowPictureButton
	 */	
	public JButton getShowPictureButton(){
		if (showPictureButton == null){
			showPictureButton = new JButton();
			showPictureButton.setBounds(new Rectangle(140, 102, 70, 26));
			showPictureButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					JFrame picture = new JFrame();
					picture.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
					picture.setBounds(Settings.Screen_x, Settings.Screen_y, 400, 300 );
					picture.setTitle("Product Picture");
					
					JLabel backgroundLabel = new JLabel("");
					backgroundLabel.setIcon(new ImageIcon("C:/prodPic.jpg"));
					backgroundLabel.setBounds(0, 0, 400, 330);
					picture.add(backgroundLabel);
					
					picture.setVisible( true );
					
					
				}
			});
			showPictureButton.setText("Show");
		}	
		return showPictureButton;				
	}

	/**
	 * This function add Save Button to product form
	 */
	public void addSaveButton(){
		this.add(getSaveButton(),null);
	}
	
	/**
	 * This function add update Button to product form
	 */
	public void addUpdateButton(){
		this.add(getUpdateButton(),null);
		pCnumTextField.setEditable(false);	
	}
	
	/**
	 * This function add Remove Button to product form
	 */
	public void addRemoveButton(){
		this.add(getRemoveButton(),null);
		pCnumTextField.setEditable(false);	
		pNameField.setEditable(false);	
		pPriceTextField.setEditable(false);	
		pDescTextArea.setEditable(false);	
	}
	
	/**
	 * This function add Show Picture Button to product form
	 */
	public void addShowPictureButton(){
		this.add(getShowPictureButton(),null);	
	}

	/**
	 * This initializes getLabel_1 (Background Label)
	 */
	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("");
			label_1.setIcon(new ImageIcon(ProductWindow.class.getResource("/Pictures/add.png")));
			label_1.setBounds(10, 171, 113, 65);
		}
		return label_1;
	}
}
